import {ref, watch} from 'vue'
import request from '@/utils/request'
import { map } from 'lodash';
export default function useNodeInfo({
  nodeInfo:node,
  selectTime,
  srcNodes,
  drawerType,
  systemId
}) {
  const nodeDetail = ref({})
  const getWorkloadDetail = () => {
    const nodeInfo = node.value
    console.log(nodeInfo, 'nodeInfo', srcNodes);
    const {from,to} = selectTime.value
    const params = {
      dstMasterIp: nodeInfo?.masterIp,
      dstNamespace: nodeInfo?.namespace,
      dstWorkloadName: nodeInfo?.workloadName,
      dstWorkloadKind: nodeInfo?.workloadKind,
      from,
      to,
      systemId,
      srcWorkloadList: map(srcNodes.value, (node) => {
        return {
          masterIp: node.masterIp,
          namespace: node.namespace,
          workloadKind: node.workloadKind,
          workloadName: node.workloadName,
        };
      }),
    };
    request.post("/k8s-workload/simple/workload/info", params).then((res) => {
      // setWorkloadInfo(res.data.data || {});
      nodeDetail.value = res.data || {}
      console.log(nodeDetail.value, 'nodeDetail.value');

    });
  };
  const getPodInfo = () => {
    const nodeInfo = node.value
    const { from, to } = selectTime.value;
    const params = {
      masterIp: nodeInfo?.masterIp,
      namespace: nodeInfo?.namespace,
      workload: nodeInfo?.workloadName,
      pod: nodeInfo?.podName,
      from,
      to,
    };
    request.get('/k8s-pod/getPodSimple', {params}).then((res) => {
      // console.log(res, 'res');
      // setWorkloadInfo(res.data.data || {});
      nodeDetail.value = res.data || {}
    });
  };
  watch([selectTime, node], ([selectTime, nodeInfo]) => {
    console.log(selectTime, 'selectTime', nodeInfo);

    if (nodeInfo && selectTime.from && drawerType.value === "workload") {
      getWorkloadDetail();
    } else if (nodeInfo && drawerType.value === "pod") {
      getPodInfo();
    }
  })
  return nodeDetail
}
